/*
 * @Author: zwz
 * @Date: 2023-05-25 10:44:26
 * @LastEditors: zwz
 * @LastEditTime: 2023-06-06 11:22:57
 * @Description: 请填写简介
 */
'use strict';

const { Controller } = require('egg');

class HomeController extends Controller {
  async index() {
    // const sql = 'select * from  user';
    // const res = await this.app.mysql.query(sql);
    // console.log(res);
    // console.log(this.service, 'this');
    // const res = await this.service.home.find();

    const { ctx, app } = this;
    await ctx.render('home.html', {
      nowTime:app.timeProp,
      username: '施浩',
      skill: '砸墙，砌墙，水电工',
    });
    // ctx.body = res;
  }
  async find() {
    const { ctx, app } = this;
    const res = await ctx.service.home.find()
    ctx.body = res
  }
  async add() {
    const ctx = this.ctx;
    ctx.cookies.set('user', '123456', {
      maxAge: 1000 * 60, // cookie的使用时长 毫秒级别
      httpOnly: true, // true 只允许服务端进行修改和查看 cookie
      encrypt: true, // 加密
    });
    ctx.body = {
      status: 200,
      data: 'Cookie添加成功',
    };
  }
  async del() {
    const ctx = this.ctx;
    ctx.cookies.set('user', null);
    ctx.body = {
      status: 200,
      data: 'Cookie删除成功',
    };
  }
  async editor() {
    const ctx = this.ctx;
    ctx.cookies.set('user', 'bilibli');
    ctx.body = {
      status: 200,
      data: 'Cookie修改成功',
    };
  }
  async show() {
    const ctx = this.ctx;
    const user = ctx.cookies.get('user');
    console.log(user);
    ctx.body = {
      status: 200,
      data: user + 'Cookie查看成功',
    };
  }
  async newContext() {
    const {ctx} = this
    console.log(ctx.params());
    ctx.body='newcontext'
  }
  async newRequest() {
    const {ctx} = this
    const token = ctx.request.token
    ctx.body = {
      status: 200,
      body: token
    }
  }
  async newResponse() {
    const {ctx} = this
    ctx.response.token = 'zwz'
    const testBse64 = ctx.helper.base64Encode('zwz')
    ctx.body=testBse64
  }
}

module.exports = HomeController;
